package com.chb.rpc;

import com.chb.entity.RpcRequest;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @description:
 * @author: CHB
 * @create: 2022-03-14 10:37
 **/
public class RemoteInvocationHandler implements InvocationHandler {

    private RpcNetTransport transport;

    public RemoteInvocationHandler(String host, Integer port){
        transport = new RpcNetTransport(host, port);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        // 封装请求参数
        RpcRequest rpcRequest = new RpcRequest();
        rpcRequest.setClassName(method.getDeclaringClass().getName());
        rpcRequest.setMethodName(method.getName());
        rpcRequest.setParameters(args);
        // 发送请求
        return transport.remoteCall(rpcRequest);
    }
}
